package com.java.Jdbc;

import java.sql.*;

/**
 * DriverManagerDemo演示了如何使用DriverManager连接到MySQL数据库并创建Statement
 */
public class DriverManagerDemo {

    public static final String url = "jdbc:mysql://localhost:3306/mydb?serverTimezone=GMT&useUnicode=true&characterEncoding=UTF-8&useSSL=false";
    public static final String username = "root";
    public static final String password = "root";

    public static void main(String[] args) {
        // 创建连接
        try (Connection connection = DriverManager.getConnection(url, username, password)) {
            System.out.println(connection);
            // 创建命令对象
            Statement statement = connection.createStatement();
            System.out.println(statement);
            //执行SQL语句
            //int rs = statement.executeUpdate("insert  into product(name,price,address,type) values ('荣耀手机',5800,'南京','手机')");
            //执行查询语句，返回结果集
            ResultSet resSet = statement.executeQuery("select * from product");
            //循环读取结果集
            while (resSet.next()) {
                int id = resSet.getInt("id");
                String name = resSet.getString("name");
                double price = resSet.getDouble("price");
                String address = resSet.getString("address");
                String type = resSet.getString("type");
                System.out.println(id + " " + name + " " + price + " " + address + " " + type);
            }

            //System.out.println("受影响的行数："+rs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
   